/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import Control.Spil;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Timer;
import model.Spørgsmål;

/**
 *
 * @author Mikkel
 */
public class SpørgsmålPanel extends javax.swing.JPanel {

    private MainGUI mainGUI;
    private Spil spil;
    private Spørgsmål sp;
    private boolean svaret;
    private double t;
    private Timer timer;

    /**
     * Creates new form Spørgsmål
     */
    public SpørgsmålPanel(MainGUI mainGUI) {
        initComponents();
        this.mainGUI = mainGUI;
        svaret = false;
        updateSpørgsmål();
    }

    public void updateSpørgsmål() {
        sp = mainGUI.getTilfældigtSpørgsmål();
        spørgsmåltext.setText(sp.getSpørgsmål());
        svar1Knap.setText(sp.getSvar1());
        svar2Knap.setText(sp.getSvar2());
        svar3Knap.setText(sp.getSvar3());
        svar4Knap.setText(sp.getSvar4());
        reset();

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        spørgsmåltext = new javax.swing.JTextArea();
        svar1Knap = new javax.swing.JButton();
        svar2Knap = new javax.swing.JButton();
        svar3Knap = new javax.swing.JButton();
        svar4Knap = new javax.swing.JButton();
        jProgressBar1 = new javax.swing.JProgressBar();

        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });

        spørgsmåltext.setEditable(false);
        spørgsmåltext.setColumns(20);
        spørgsmåltext.setRows(5);
        spørgsmåltext.setOpaque(false);
        jScrollPane1.setViewportView(spørgsmåltext);

        svar1Knap.setText("jButton1");
        svar1Knap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                svar1KnapActionPerformed(evt);
            }
        });

        svar2Knap.setText("jButton1");
        svar2Knap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                svar2KnapActionPerformed(evt);
            }
        });

        svar3Knap.setText("jButton1");
        svar3Knap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                svar3KnapActionPerformed(evt);
            }
        });

        svar4Knap.setText("jButton1");
        svar4Knap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                svar4KnapActionPerformed(evt);
            }
        });

        jProgressBar1.setBackground(new java.awt.Color(0, 255, 0));
        jProgressBar1.setValue(100);
        jProgressBar1.setOpaque(true);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 460, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 460, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(svar1Knap, javax.swing.GroupLayout.PREFERRED_SIZE, 204, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(52, 52, 52)
                        .addComponent(svar2Knap, javax.swing.GroupLayout.PREFERRED_SIZE, 204, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(svar3Knap, javax.swing.GroupLayout.PREFERRED_SIZE, 204, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(52, 52, 52)
                        .addComponent(svar4Knap, javax.swing.GroupLayout.PREFERRED_SIZE, 204, javax.swing.GroupLayout.PREFERRED_SIZE))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(6, 6, 6)
                .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(svar1Knap, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(svar2Knap, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(svar3Knap, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(svar4Knap, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void svar1KnapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_svar1KnapActionPerformed
        svaret = true;
        if (mainGUI.tjekSvar(svar1Knap.getText())) {
            svar1Knap.setBackground(Color.green);
        } else {
            svar1Knap.setBackground(Color.red);
        }
        svar1Knap.paint(svar1Knap.getGraphics());

        svar2Knap.setEnabled(false);
        svar3Knap.setEnabled(false);
        svar4Knap.setEnabled(false);


        try {
            Thread.sleep(2000);
        } catch (InterruptedException ex) {
            Logger.getLogger(SpørgsmålPanel.class.getName()).log(Level.SEVERE, null, ex);
        }

        updateSpørgsmål();
    }//GEN-LAST:event_svar1KnapActionPerformed

    private void svar2KnapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_svar2KnapActionPerformed
        svaret = true;
        if (mainGUI.tjekSvar(svar2Knap.getText())) {
            svar2Knap.setBackground(Color.green);
        } else {
            svar2Knap.setBackground(Color.red);
        }
        svar2Knap.paint(svar2Knap.getGraphics());
        try {
            Thread.sleep(2000);
        } catch (InterruptedException ex) {
            Logger.getLogger(SpørgsmålPanel.class.getName()).log(Level.SEVERE, null, ex);
        }

        svar1Knap.setEnabled(false);
        svar3Knap.setEnabled(false);
        svar4Knap.setEnabled(false);
        updateSpørgsmål();
    }//GEN-LAST:event_svar2KnapActionPerformed

    private void svar3KnapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_svar3KnapActionPerformed
        svaret = true;
        if (mainGUI.tjekSvar(svar3Knap.getText())) {
            svar3Knap.setBackground(Color.green);
        } else {
            svar3Knap.setBackground(Color.red);
        }
        svar3Knap.paint(svar3Knap.getGraphics());
        try {
            Thread.sleep(2000);
        } catch (InterruptedException ex) {
            Logger.getLogger(SpørgsmålPanel.class.getName()).log(Level.SEVERE, null, ex);
        }

        svar1Knap.setEnabled(false);
        svar2Knap.setEnabled(false);
        svar4Knap.setEnabled(false);
        updateSpørgsmål();
    }//GEN-LAST:event_svar3KnapActionPerformed

    private void svar4KnapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_svar4KnapActionPerformed
        svaret = true;
        if (mainGUI.tjekSvar(svar4Knap.getText())) {
            svar4Knap.setBackground(Color.green);
        } else {
            svar4Knap.setBackground(Color.red);
        }
        svar4Knap.paint(svar4Knap.getGraphics());
        try {
            Thread.sleep(2000);
        } catch (InterruptedException ex) {
            Logger.getLogger(SpørgsmålPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
        svar1Knap.setEnabled(false);
        svar2Knap.setEnabled(false);
        svar3Knap.setEnabled(false);
        updateSpørgsmål();
    }//GEN-LAST:event_svar4KnapActionPerformed

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
//        reset();
    }//GEN-LAST:event_formComponentShown

    private void reset() {

        svar1Knap.setBackground(new Color(240, 240, 240));
        svar2Knap.setBackground(new Color(240, 240, 240));
        svar3Knap.setBackground(new Color(240, 240, 240));
        svar4Knap.setBackground(new Color(240, 240, 240));
        svar1Knap.setEnabled(true);
        svar2Knap.setEnabled(true);
        svar3Knap.setEnabled(true);
        svar4Knap.setEnabled(true);
        t = 15;
        timer = new Timer(1000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                t--;
                jProgressBar1.setValue(100 - (int) (((15 - t) / 15) * 100));
                jProgressBar1.repaint();

                System.out.println(t + " bliver omregnet til " + ((15 - t) / 15) * 100 + "%");
                if (t <= 0) {

                    mainGUI.tjekSvar(" ");
                    if (mainGUI.tjekSvarUdenpoint(svar1Knap.getText())) {
                        svar1Knap.setBackground(Color.green);
                    } else {
                        svar1Knap.setBackground(Color.red);
                    }
                    if (mainGUI.tjekSvarUdenpoint(svar2Knap.getText())) {
                        svar2Knap.setBackground(Color.green);
                    } else {
                        svar2Knap.setBackground(Color.red);
                    }
                    if (mainGUI.tjekSvarUdenpoint(svar3Knap.getText())) {
                        svar3Knap.setBackground(Color.green);
                    } else {
                        svar3Knap.setBackground(Color.red);
                    }
                    if (mainGUI.tjekSvarUdenpoint(svar4Knap.getText())) {
                        svar4Knap.setBackground(Color.green);
                    } else {
                        svar4Knap.setBackground(Color.red);
                    }

                    svar1Knap.paint(svar1Knap.getGraphics());
                    svar2Knap.paint(svar2Knap.getGraphics());
                    svar3Knap.paint(svar3Knap.getGraphics());
                    svar4Knap.paint(svar4Knap.getGraphics());
                    svar1Knap.setEnabled(false);
                    svar2Knap.setEnabled(false);
                    svar3Knap.setEnabled(false);
                    svar4Knap.setEnabled(false);
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(SpørgsmålPanel.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    svaret = true;

                    updateSpørgsmål();

                } else {
//                    if (svaret) {
//                        timer.stop();
//                    }
                }
            }
        });
        if (!svaret) {
            timer.start();
            System.out.println("VI STARTER TIMEREN");
        }
        svaret = false;


    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JProgressBar jProgressBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea spørgsmåltext;
    private javax.swing.JButton svar1Knap;
    private javax.swing.JButton svar2Knap;
    private javax.swing.JButton svar3Knap;
    private javax.swing.JButton svar4Knap;
    // End of variables declaration//GEN-END:variables
}
